﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TeenyGrab.Windows;
using TeenyGrab.Helpers;

namespace TeenyGrab.Inputs
{
    /// <summary>
    /// Interaction logic for ConfigurePartialScreenInputWindow.xaml
    /// </summary>
    public partial class ConfigurePartialScreenInputWindow : Window
    {
        public PartialScreenInput Input { get; set; }
        public ConfigurePartialScreenInputWindow(PartialScreenInput input)
        {
            InitializeComponent();

            Input = input;

            textBoxLeft.Text =   "" + input.Area.X;
            textBoxTop.Text =    "" + input.Area.Y;
            textBoxWidth.Text =  "" + input.Area.X2;
            textBoxHeight.Text = "" + input.Area.Y2;

            checkBoxPickEveryTime.IsChecked = input.PickEveryTime;
        }

        private void buttonPick_Click(object sender, RoutedEventArgs e)
        {
            var areaSelector = new AreaSelectorWindow(new ScreenCapture().CaptureAllScreens());
            if (areaSelector.ShowDialog() == true)
            {
                textBoxLeft.Text   = "" + areaSelector.Area.X;
                textBoxTop.Text    = "" + areaSelector.Area.Y;
                textBoxWidth.Text  = "" + areaSelector.Area.X2;
                textBoxHeight.Text = "" + areaSelector.Area.Y2;
            }
        }

        private void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            int x = 0, y = 0, w = 0, h = 0;
            if (Int32.TryParse(textBoxLeft.Text, out x) &&
               Int32.TryParse(textBoxTop.Text, out y) &&
               Int32.TryParse(textBoxWidth.Text, out w) &&
               Int32.TryParse(textBoxHeight.Text, out h))
            {
                Input.Area = new Area(x, y, w, h);
                Input.PickEveryTime = (bool)checkBoxPickEveryTime.IsChecked;

                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show(this, "Error", "Left, Top, Width and Height must be valid integers.", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
